package com.intxt.bbs.config;

import com.intxt.bbs.controller.*;
import com.intxt.bbs.model.*;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;

public class Config extends JFinalConfig {

    private Logger log = Logger.getLogger(Config.class);

	@Override
	public void configConstant(Constants arg0) {
		loadPropertyFile("config.properties");
		arg0.setDevMode(getPropertyToBoolean("isDev",true));
		arg0.setViewType(ViewType.JSP);
	}

	@Override
	public void configHandler(Handlers arg0) {
		arg0.add(new ContextPathHandler("ctx"));
	}	

	@Override
	public void configInterceptor(Interceptors arg0) {
	
	}

	@Override
	public void configPlugin(Plugins arg0) {
        String datasource_url = getProperty("datasource_url");
        String datasource_username = getProperty("datasource_username");
        String datasource_password = getProperty("datasource_password");
        log.debug("数据库连接地址:"+datasource_url);
        log.debug("数据库连接用户名:"+datasource_username);
        log.debug("数据库连接密码:"+datasource_password);
		DruidPlugin dp = new DruidPlugin(datasource_url, datasource_username, datasource_password);
		dp.setFilters("stat");
		arg0.add(dp);
		ActiveRecordPlugin acp = new ActiveRecordPlugin(dp);
		acp.setShowSql(true);
		
		acp.addMapping("intxt_user", User.class);
		acp.addMapping("intxt_user_group", UserGroup.class);
		acp.addMapping("intxt_link", Link.class);
		acp.addMapping("intxt_area", Area.class);
		acp.addMapping("intxt_topic", Topic.class);
		acp.addMapping("intxt_reply", Reply.class);
		acp.addMapping("intxt_label", TopicLabel.class);

		arg0.add(acp);
	}

	@Override
	public void configRoute(Routes arg0) {
		arg0.add("/topic", TopicController.class);
		arg0.add("/area", AreaController.class);
		arg0.add("/user", UserController.class);
		arg0.add("/link", LinkController.class);
		arg0.add("/reply", ReplyController.class);
		arg0.add("/admin", AdminController.class);
		arg0.add("/", IndexController.class);
	}

}
